/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhaosj
 * Date: 2010-11-30 下午03:19:52
 */
package com.webservice;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

/**
 * @author zhaosj
 * 
 */
public class JsonService {

	/**
	 * List
	 */
	public String listToJson() {
		boolean[] boolArray = new boolean[] { true, false, true };
		JSONArray jsonArray1 = JSONArray.fromObject(boolArray);
		System.out.println(jsonArray1);

		List list = new ArrayList();
		list.add("first");
		list.add("second");

		JSONArray jsonArray2 = JSONArray.fromObject(list);
		System.out.println(jsonArray2);

		JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");
		System.out.println(jsonArray3);

		return null;
	}

	/**
	 * Map
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String mapToJson() {
		Map map = new HashMap();
		map.put("name", "json1");
		map.put("bool", Boolean.TRUE);

		map.put("int", new Integer(1));
		map.put("arr", new String[] { "a", "b" });
		map.put("func", "function(i){ return this.arr[i]; }");

		Map map2 = new HashMap();
		map2.put("name", "json2");
		map2.put("bool", Boolean.TRUE);

		map2.put("int", new Integer(1));
		map2.put("arr", new String[] { "c", "d" });
		map2.put("func", "function(j){ return this.arr[j]; }");
		List list = new ArrayList();

		list.add(map);
		list.add(map2);

		// JSONObject json = JSONObject.fromObject(map);
		// System.out.println(json);

		JSONArray json2 = JSONArray.fromObject(list);
		// System.out.println(json2);

		return json2.toString();
	}

	public String getJsonStr() {
		String aStrVar = null;
		try {
			DbConnection dc = new DbConnection();
			// Connection con = dc.connectDbByThin();
			Connection con = dc.getCon();
			String sql = "select * from ma_stock where innercode = ? and tradingday = (select max(tradingday) from ma_stock where innercode = ?)";
			PreparedStatement pst = con.prepareStatement(sql);
			pst.setInt(1, 3);
			pst.setInt(2, 3);
			ResultSet rs = pst.executeQuery();

			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("sucucode", rs.getString("secucode"));
				map.put("closeprice", rs.getDouble("closeprice"));
				map.put("tradingday", sdf.format(rs.getDate("tradingday")));

				list.add(map);
			}

			JSONArray json = JSONArray.fromObject(list);
			aStrVar = json.toString();

			rs.close();
			pst.close();
			con.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return aStrVar;
	}

	public String getJyStr() {
		String aStrVar = null;
		try {
			DbConnection dc = new DbConnection();
			//Connection con = dc.connectDbByThin();
			Connection con = dc.getJyCon();
			String sql = "select *  from jydb.secumain s where (substr(s.secucode, 1, 2) = '00' and s.secumarket = 90)    or (substr(s.secucode, 1, 2) = '60' and s.secumarket = 83) order by innercode";
			PreparedStatement pst = con.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();

			List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
			while (rs.next()) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("secucode", rs.getString("secucode"));
				map.put("chiname", rs.getString("chiname"));
				map.put("secuabbr", rs.getString("secuabbr"));
				map.put("chispelling", rs.getString("chispelling"));

				list.add(map);
			}

			JSONArray json = JSONArray.fromObject(list);
			aStrVar = json.toString();

			rs.close();
			pst.close();
			con.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return aStrVar;
	}

	public static void main(String[] args) {
		JsonService jt = new JsonService();
		String str = jt.getJyStr();

		System.out.println(str);
	}

}
